<?php
@ob_start();
@session_start();

include("../connect/connect.php");
include("../function/thaidate.php");
include("../function/config.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>-</title>
<link rel="stylesheet" href="stylereport.css" type="text/css" />
<link rel="shortcut icon" href="../image/Letter-T.ico" />
</head>
<body>
<?php
if($_POST['rtype']=='day' && $_POST['dateInput']!=""){
//เลือกวันที่
	$dmy = $_POST["dateInput"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	//$date = ($y - 543) . '-' . $m . '-' . $d;
	//$date = date("Y-m-d", $adate);

	$adate = $year . '-' . $month . '-' . $day;

	if($_POST['selectall']=='selectall'){
		$Query = mysql_query("SELECT sale.sale_datetime3 as sale_datetime3,member.member_id as member_id,member.member_title_name as member_title_name,member.member_name as member_name,member.member_surname as member_surname FROM sale,member WHERE sale.member_id = member.member_id AND stype_id = '2' AND sale.sale_datetime3 = '".$adate."' GROUP BY member.member_id ORDER BY member.member_name,member.member_surname");
	}else if($_POST['name']){
		$name = explode(" ",$_POST['name']);
		$Query = mysql_query("SELECT product.prod_name,sum(sale.sale_qty) as sale_qty,sale.sale_datetime3 as sale_datetime3,sale.sale_price as sale_price,sum(sale.sale_discount) as sale_discount,sum(sale.sale_service) as sale_service FROM member,sale,product WHERE sale.member_id = member.member_id AND product.prod_id = sale.prod_id AND sale.stype_id = '2' AND sale.sale_datetime3 = '".$adate."' AND ((member.member_name = '".$name[0]."' AND member.member_surname = '".$name[1]."') OR member.member_code = '".$_POST['name']."' ) GROUP BY sale.prod_id,sale.sale_price ORDER BY product.prod_name");
		$nQuery = mysql_query("SELECT member_title_name,member_name,member_surname FROM member WHERE ((member.member_name = '".$name[0]."' AND member.member_surname = '".$name[1]."') OR member.member_code = '".$_POST['name']."' ) ");
	}else{
		echo "กรุณาเลือกชื่อสมาชิกด้วยค่ะ";
	}
	
}else if($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!=""){
//เลือกเดือน	
	if($_POST['selectall']=='selectall'){
		$Query = mysql_query("SELECT sale.sale_datetime3 as sale_datetime3,member.member_id as member_id,member.member_title_name as member_title_name,member.member_name as member_name,member.member_surname as member_surname FROM sale,member WHERE sale.member_id = member.member_id AND stype_id = '2' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY member.member_id ORDER BY member.member_name,member.member_surname");
	}else if($_POST['name']){
		$name = explode(" ",$_POST['name']);
		$Query = mysql_query("SELECT product.prod_name,sum(sale.sale_qty) as sale_qty,sale.sale_datetime3 as sale_datetime3,sale.sale_price as sale_price,sum(sale.sale_discount) as sale_discount,sum(sale.sale_service) as sale_service FROM member,sale,product WHERE sale.member_id = member.member_id AND product.prod_id = sale.prod_id AND sale.stype_id = '2' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' AND ((member.member_name = '".$name[0]."' AND member.member_surname = '".$name[1]."') OR member.member_code = '".$_POST['name']."' ) GROUP BY sale.prod_id,sale.sale_price,sale.sale_datetime3 ORDER BY sale.sale_datetime3");
		$nQuery = mysql_query("SELECT member_title_name,member_name,member_surname FROM member WHERE ((member.member_name = '".$name[0]."' AND member.member_surname = '".$name[1]."') OR member.member_code = '".$_POST['name']."' ) ");
	}else{
		echo "กรุณาเลือกชื่อสมาชิกด้วยค่ะ";
	}

}else if($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!=""){
//เลือกช่วงเวลา
	$dmy = $_POST["dateInput1"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	$date1 = $year."-".$month."-".$day;
	
	$dmy1 = $_POST["dateInput2"];
	$day1 = substr($dmy1, 0, 2);
	$month1 = substr($dmy1, 3, 2);
	$y1 = substr($dmy1, 6, 4);
	$year1 = $y1-543;
	$date2 = $year1."-".$month1."-".$day1;
	
	if($_POST['selectall']=='selectall'){
		$Query = mysql_query("SELECT sale.sale_datetime3 as sale_datetime3,member.member_id as member_id,member.member_title_name as member_title_name,member.member_name as member_name,member.member_surname as member_surname FROM sale,member WHERE sale.member_id = member.member_id AND stype_id = '2' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY member.member_id ORDER BY member.member_name,member.member_surname");
	}else if($_POST['name']){
		$name = explode(" ",$_POST['name']);
		$Query = mysql_query("SELECT product.prod_name,sum(sale.sale_qty) as sale_qty,sale.sale_datetime3 as sale_datetime3,sale.sale_price as sale_price,sum(sale.sale_discount) as sale_discount,sum(sale.sale_service) as sale_service FROM member,sale,product WHERE sale.member_id = member.member_id AND product.prod_id = sale.prod_id AND sale.stype_id = '2' AND ((member.member_name = '".$name[0]."' AND member.member_surname = '".$name[1]."') OR member.member_code = '".$_POST['name']."' ) AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price,sale.sale_datetime3 ORDER BY sale.sale_datetime3");
		$nQuery = mysql_query("SELECT member_title_name,member_name,member_surname FROM member WHERE ((member.member_name = '".$name[0]."' AND member.member_surname = '".$name[1]."') OR member.member_code = '".$_POST['name']."' ) ");
	}else{
		echo "กรุณาเลือกชื่อสมาชิกด้วยค่ะ";
	}
}
$nRow = mysql_fetch_array($nQuery);
?>
<font size="5"><strong><?php echo $report['header'];?></strong></font><br>
<font size="5"><strong>รายงานสมาชิก</strong></font><br><br>
<font size="4"><strong><?php 
if($_POST['rtype']=='day'){
	echo "ประจำ".thaidate(strtotime($adate));
} else if($_POST['rtype']=='month'){
	$monthname = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
	$year = $_POST['yearSelect']+543;
	echo "ประจำเดือน ".$monthname[$_POST['monthSelect']-1]." พ.ศ. ". $year ;
} else if($_POST['rtype']=='dateto'){
	echo "ตั้งแต่วันที่ ".thai_date(strtotime($date1))." ถึงวันที่ ".thai_date(strtotime($date2));
}
 ?></strong></font><br>
<?php if($_POST['selectall']=='selectall'){ 
$i=0;
while($bRow=mysql_fetch_array($Query)){
	$i++;
?>
<br /><font size="4"><strong><?php echo $bRow['member_title_name'].$bRow['member_name']." ".$bRow['member_surname']; ?></strong></font><br /><br />

<table class="listorder">
  <thead>
<tr>
<td width="1">ลำดับ</td>
<?php if($_POST['rtype']!='day'){?>
<td width="12%">วันที่</td>
<?php } ?>
<td>รายการ</td>
<td width="9%">หน่วยละ</td>
<td width="7%">จำนวน</td>
<td width="10%">ราคาขาย</td>
<td width="10%">ค่าบริการ</td>
<td width="8%">ส่วนลด</td>
<td width="12%">ราคารวม</td>
</tr>
  </thead>
<?php
if($_POST['rtype']=='day' && $_POST['dateInput']!=""){
	//เลือกวันที่
	$sQuery = mysql_query("SELECT product.prod_name,sum(sale.sale_qty) as sale_qty,sale.sale_datetime3 as sale_datetime3,sale.sale_price as sale_price,sum(sale.sale_discount) as sale_discount,sum(sale.sale_service) as sale_service FROM member,sale,product WHERE sale.member_id = member.member_id AND product.prod_id = sale.prod_id AND sale.stype_id = '2' AND sale.sale_datetime3 = '".$adate."' AND member.member_id = '".$bRow['member_id']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY product.prod_name");
	
}else if($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!=""){
	//เลือกเดือน
	$sQuery = mysql_query("SELECT product.prod_name,sum(sale.sale_qty) as sale_qty,sale.sale_datetime3 as sale_datetime3,sale.sale_price as sale_price,sum(sale.sale_discount) as sale_discount,sum(sale.sale_service) as sale_service FROM member,sale,product WHERE sale.member_id = member.member_id AND product.prod_id = sale.prod_id AND sale.stype_id = '2' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' AND member.member_id = '".$bRow['member_id']."' GROUP BY sale.prod_id,sale.sale_price,sale.sale_datetime3 ORDER BY sale.sale_datetime3,product.prod_name");

}else if($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!=""){
	//เลือกช่วงเวลา
	$sQuery = mysql_query("SELECT product.prod_name,sum(sale.sale_qty) as sale_qty,sale.sale_datetime3 as sale_datetime3,sale.sale_price as sale_price,sum(sale.sale_discount) as sale_discount,sum(sale.sale_service) as sale_service FROM member,sale,product WHERE sale.member_id = member.member_id AND product.prod_id = sale.prod_id AND sale.stype_id = '2' AND member.member_id = '".$bRow['member_id']."' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price,sale.sale_datetime3 ORDER BY sale.sale_datetime3,product.prod_name");
}

$num = 0;
while($row = mysql_fetch_array($sQuery)) {
	$num++;
?>
<tr>
<td align="center" valign="top" ><?php echo $num; ?></td>
<?php if($_POST['rtype']!='day'){ ?>
<td align="right" valign="top" style="padding-right:5px"><?php echo thai_date2(strtotime($row['sale_datetime3'])); ?></td>
<?php }?>
<td align="left" valign="top" style="padding-left:5px"><?php echo $row['prod_name']; ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php echo number_format($row['sale_price'], 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:10px"><?php echo $row['sale_qty']; ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $total = $row['sale_price']*$row['sale_qty']; echo number_format($total, 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $service = $row['sale_service']; echo number_format($service, 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $discount = $row['sale_discount']; echo number_format($discount, 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $sale_discount = ($total+$service)-$discount;
echo number_format($sale_discount, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal[$i]=$strTotal[$i]+$sale_discount;
}
?>
<tfoot>
  <tr>
    <td colspan="<?php if($_POST['rtype']=='day'){ echo "7";}else{echo "8";} ?>" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:5px"><?php echo number_format($strTotal[$i], 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>
<?php } }else if($_POST['name']){ ?>

<br /><font size="4"><strong><?php echo $nRow['member_title_name'].$nRow['member_name']." ".$nRow['member_surname']; ?></strong></font><br /><br />

<table class="listorder">
  <thead>
<tr>
<td width="1">ลำดับ</td>
<?php if($_POST['rtype']!='day'){?>
<td width="12%">วันที่</td>
<?php } ?>
<td>รายการ</td>
<td width="9%">หน่วยละ</td>
<td width="7%">จำนวน</td>
<td width="10%">ราคาขาย</td>
<td width="10%">ค่าบริการ</td>
<td width="8%">ส่วนลด</td>
<td width="12%">ราคารวม</td>
</tr>
  </thead>
<?php
$num = 0;
while($row = mysql_fetch_array($Query)) {
	$num++;
?>
<tr>
<td align="center" valign="top" ><?php echo $num; ?></td>
<?php if($_POST['rtype']!='day'){ ?>
<td align="right" valign="top" style="padding-right:5px"><?php echo thai_date2(strtotime($row['sale_datetime3'])); ?></td>
<?php }?>
<td align="left" valign="top" style="padding-left:5px"><?php echo $row['prod_name']; ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php echo number_format($row['sale_price'], 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:10px"><?php echo $row['sale_qty']; ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $total = $row['sale_price']*$row['sale_qty']; echo number_format($total, 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $service = $row['sale_service']; echo number_format($service, 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $discount = $row['sale_discount']; echo number_format($discount, 2,'.',','); ?></td>
<td align="right" valign="top" style="padding-right:5px"><?php $sale_discount = ($total+$service)-$discount;
echo number_format($sale_discount, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal=$strTotal+$sale_discount;
}
?>
<tfoot>
<?php if($num>='1'){?>
  <tr>
    <td colspan="<?php if($_POST['rtype']=='day'){ echo "7";}else{echo "8";} ?>" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:5px"><?php echo number_format($strTotal, 2,'.',','); ?></td>
	</tr>
<?php } else { ?>
  <tr>
    <td colspan="<?php if($_POST['rtype']=='day'){ echo "8";}else{echo "9";} ?>" align="center">ไม่มีรายการ</td>
	</tr>
<?php } ?>
</tfoot>
</table>
<?php } ?>
<div style="padding-top: 20px">
<input type="button" name="endsale" id="endsale" value="พิมพ์รายงาน" onclick="javascript:window.print();" > <input type="button" name="btnClose" value="ปิดหน้าต่าง" onclick="JavaScript:window.close();" /></div>
</body>

</html>
